package fr.lille.inria.adam.lifl.EcoGardeInventory.Activities;

import java.util.Arrays;
import java.util.Observable;
import java.util.Observer;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import fr.lille.inria.adam.lifl.EcoGardeInventory.R;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Utils.ElasticNumberPicker;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Utils.sparse.SparseAdapter;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Utils.sparse.SparseEntry;
import fr.lille.inria.adam.lifl.EcoGardeInventory.Utils.sparse.SparseEntryKeyCompareBuilder;

public class QuantizePopulation extends BuildObservationActivity {
	protected double populationSizeAccumulator = 0d;
	protected TextView populationSizeOut;
	protected SeekBar populationSizeBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.select_population_size);
		
		// On fait le branchement du comportement de la SeekBar
		// qui servira � quantifier la taille de la population
		initializeSizePicker();
	}
	
	private int getCurrentPopulationSize() { 
		return (int) (Math.signum(this.populationSizeAccumulator)*Math.abs(this.populationSizeAccumulator)); }
	
	private void refreshPopulationSizeTextOut() {
		int size = getCurrentPopulationSize();
		populationSizeOut.setText(String.format("%d", size));
	}
	
	private void initializeSizePicker() {
		((Button) findViewById(R.id.increase_population_size)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) { populationSizeAccumulator +=1; refreshPopulationSizeTextOut(); }
		});
		((Button) findViewById(R.id.decrease_population_size)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) { populationSizeAccumulator -=1; refreshPopulationSizeTextOut(); }
		});
		
		populationSizeOut = (TextView) findViewById(R.id.select_population_sizeOut);
		this.populationSizeBar = (SeekBar) findViewById(R.id.seekbar_population_size);
		refreshPopulationSizeTextOut();
		this.populationSizeBar.setOnSeekBarChangeListener(new ElasticNumberPicker(new Observer() {
			@Override
			public void update(Observable observable, Object data) {
				populationSizeAccumulator += (Double) data;
				populationSizeAccumulator = Math.max(populationSizeAccumulator, 0);
				
				// Android interdit des manipulation graphique
				// autrement que par le Thread graphique
				Runnable onUI = new Runnable() {
					@Override
					public void run() {
						refreshPopulationSizeTextOut();
					}
				};
				populationSizeBar.post(onUI);
			}
		}));
	}

	private void initializePrecisionPicker() {
		SparseArray<String> precisions = this.clientBinder.retrievePrecisions(this.observationEnCoursDeRealisation.groupeEspece);
		@SuppressWarnings("unchecked")
		SparseEntry<String>[] precArray = new SparseEntry[precisions.size()];
		SparseEntry.toArray(precisions, precArray, new SparseEntryKeyCompareBuilder<String>());
		Arrays.sort(precArray);
		SparseAdapter precAdapter = new SparseAdapter(getApplicationContext(), 
				R.layout.select_listitem, 
				precArray);
		
		final ListView precisionListView = (ListView) findViewById(R.id.select_population_precision);
		precisionListView.setAdapter(precAdapter);
		
		precisionListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				observationEnCoursDeRealisation.populationSize = getCurrentPopulationSize();
				observationEnCoursDeRealisation.populationSizePrecision = (int) id;
				startNextActivity();
			}
		});
	}
	
	@Override
	protected ServiceConnection initializeClientSIGServConn() {
		return new ServiceConnection() {
			
			@Override
			public void onServiceDisconnected(ComponentName name) { }
			
			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				// Branchement comportement pour la pr�cision
				initializePrecisionPicker();
			}
		};
	}
	
	private void startNextActivity() {
		Intent selectObsIntent = new Intent(getApplicationContext(), CategorizePopulation.class);
		selectObsIntent.putExtra("SIG_OBSERVATION", observationEnCoursDeRealisation);
		selectObsIntent.putExtra("SIG_USER", user);
		selectObsIntent.putExtra(SelectStringEntryOnList.extraIntentKey_STEP, SelectStringEntryOnList.TYPE_OBSERVATEUR);
		startActivity(selectObsIntent);
	}
}
